package com.un.ebs.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.sys.domain.AppTenant;
import com.un.ebs.sys.domain.AppTenantExt;
import com.un.ebs.sys.domain.TenantAdmin;
import com.un.ebs.sys.dto.AppTenantAddDto;
import com.un.ebs.sys.dto.AppTenantUpdateDto;
import com.un.ebs.sys.mapper.AppTenantExtMapper;
import com.un.ebs.sys.mapper.AppTenantMapper;
import com.un.ebs.sys.mapper.TenantAdminMapper;
import com.un.ebs.sys.service.AppTenantService;
import com.un.ebs.sys.service.PersonnelService;
import com.un.ebs.sys.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liubo
 * @since 2019-09-15
 */
@Service
public class AppTenantServiceImpl extends ServiceImpl<AppTenantMapper, AppTenant> implements AppTenantService {

    @Resource
    private TenantAdminMapper tenantAdminMapper;

    @Resource
    private AppTenantExtMapper appTenantExtMapper;

    @Resource
    private UserService userService;

    @Resource
    private PersonnelService personnelService;

    @Transactional
    @Override
    public void add(AppTenantAddDto req) {
        AppTenant obj = new AppTenant();
        obj.setAppId(req.getAppId());
        obj.setEntId(req.getEntId());
        obj.setStartDate(req.getStartDate());
        obj.setEndDate(req.getEndDate());
        obj.setIsDelete(false);
        obj.insert();

        TenantAdmin admin = new TenantAdmin();
        admin.setTenantId(obj.getId());
        admin.setUserId(SecurityUtils.getUserContent().getId());
        admin.insert();
    }

    @Override
    public void addAdmin(String tenantId, String userId) {
        LambdaQueryWrapper<TenantAdmin> lq = Wrappers.lambdaQuery();
        lq.eq(TenantAdmin::getTenantId, tenantId);
        lq.eq(TenantAdmin::getUserId, userId);
        TenantAdmin tenantAdmin = tenantAdminMapper.selectOne(lq);
        if (tenantAdmin == null) {
            tenantAdmin = new TenantAdmin();
            tenantAdmin.setTenantId(tenantId);
            tenantAdmin.setUserId(userId);
            tenantAdmin.insert();
        }
    }

    @Override
    public void addAdmin(String tenantId, String userName, String password, String personnelName, String phone) {
        personnelService.addAdmin(tenantId, userName, password, personnelName, phone);
    }

    @Override
    public void update(AppTenantUpdateDto req) {
        AppTenant obj = getById(req.getId());
        obj.setAppId(req.getAppId());
        obj.setEntId(req.getEntId());
        obj.setStartDate(req.getStartDate());
        obj.setEndDate(req.getEndDate());
        obj.updateById();
    }

    @Override
    public AppTenant queryById(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public AppTenantExt queryViewById(String id) {
        return appTenantExtMapper.selectById(id);
    }

    @Override
    public void delete(String id) {
        getBaseMapper().deleteById(id);
    }

    @Transactional
    @Override
    public void deleteAdmin(String tenantId, String userId) {
        LambdaQueryWrapper<TenantAdmin> lq = Wrappers.lambdaQuery();
        lq.eq(TenantAdmin::getTenantId, tenantId);
        lq.eq(TenantAdmin::getUserId, userId);
        tenantAdminMapper.delete(lq);

        userService.delete(userId);
    }

    @Override
    public List<String> queryTenantIdByEntId(String entID) {
        LambdaQueryWrapper<AppTenant> lq = Wrappers.lambdaQuery();
        lq.eq(AppTenant::getEntId, entID);
        lq.eq(AppTenant::getIsDelete, false);
        List<AppTenant> tenants = baseMapper.selectList(lq);
        if (tenants.size() <= 0) {
            return new ArrayList<>();
        } else {
            return tenants.stream().map(s -> s.getId()).collect(Collectors.toList());
        }
    }

}